package com.yuliang.ai.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yuliang.ai.domain.Vo.AdminUserVo;
import com.yuliang.ai.domain.entity.AdminUser;
import com.yuliang.ai.global.Constants.RedisConf;
import com.yuliang.ai.mapper.AdminUserMapper;
import com.yuliang.ai.service.AdminUserService;
import com.yuliang.utils.result.ResultUtils;
import com.yuliang.utils.result.emums.HttpCodeEnum;
import com.yuliang.utils.superbackage.SuperServiceImpl;
import com.yuliang.utils.util.RedisUtils;
import com.yuliang.utils.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author Huangkai
 */
@Slf4j
@Service
public class AdminUserServiceImpl extends SuperServiceImpl<AdminUserMapper, AdminUser> implements AdminUserService {


    @Autowired
    AdminUserService adminUserService;

    @Override
    public String login(AdminUserVo adminUserVo) {
        LambdaQueryWrapper<AdminUser> wrapper = new QueryWrapper<AdminUser>().lambda();
        wrapper.eq(AdminUser::getPhone, adminUserVo.getPhone());
        AdminUser adminUser = adminUserService.getOne(wrapper);
        log.info("查询结果{}", adminUser);
        if(adminUser == null){
            return ResultUtils.ResultWithMessageEnum(HttpCodeEnum.user_not_exist);
        }
        if(adminUser.getPassword().equals(adminUserVo.getPassword())){
            return ResultUtils.ResultWithMessageEnum(HttpCodeEnum.user_login_success);
        }
        return ResultUtils.ResultWithMessageEnum(HttpCodeEnum.user_password_error);
    }

    @Override
    public String syncToken(AdminUserVo adminUserVo) {
        if(!StringUtils.isEmpty(adminUserVo.getToken())){
            return ResultUtils.ResultWithMessageEnum(HttpCodeEnum.not_token);
        }
        //设置token
        String token = adminUserVo.getToken();
        RedisUtils.set(adminUserVo.getUserId() + RedisConf.token, token);
        return ResultUtils.ResultWithMessageEnum(HttpCodeEnum.operation_success);
    }
}
